﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;

using System.Runtime.Serialization;
using System.IO;

using Newtonsoft.Json;

using TextFile;
using MS_UI;
using MS_Enitity.EAP;
using System.Threading;
using System.Drawing;

namespace rest
{


    public delegate void SocketDelegate(int index, string msgStr);


    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MesRestService : IMesRestService
    {


        public event SocketDelegate LogEvent;




        private MS_UI.FrmMain  form = null;

        public MesRestService(MS_UI.FrmMain Myform)
        {
            this.form = Myform; 
        }

        //AGV到位通知接口：请求进入
        public AgvRequestEntryResponse AisCenterAgvAskIn(AgvRequestEntry request)
        {

            AgvRequestEntryResponse result = new AgvRequestEntryResponse();

            short value;

            if(request.callCode == CommMethods.port1Position )
            {
                LogEvent(1, "EAP下发工位1AGV请求进入命令：" + JsonConvert.SerializeObject(request));
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位1AGV请求.code"], "1");
                CommMethods.port1TaskCode = request.taskCode;
                CommMethods.port1ReqCode = request.reqCode;
                value = Convert.ToInt16(CommMethods.CurrentMelsecValue["工位1AGV请求.Index"]);
                value = (short)(value + 1);
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位1AGV请求.Index"], value.ToString());
                result.response_uuid = Guid.NewGuid().ToString(); ;
                result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                result.code = 0;
                result.msg = "成功";
                LogEvent(1, "响应--->EAP下发工位1AGV请求进入命令:  " + JsonConvert.SerializeObject(result));
            }

            if (request.callCode == CommMethods.port2Position)
            {
                LogEvent(1, "EAP下发工位2AGV请求进入命令：" + JsonConvert.SerializeObject(request));
                if (request.method == "IN")
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位2AGV请求.code"], "1");
                CommMethods.port2TaskCode = request.taskCode;
                value = Convert.ToInt16(CommMethods.CurrentMelsecValue["工位2AGV请求.Index"]);
                value = (short)(value + 1);
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位2AGV请求.Index"], value.ToString());
                result.response_uuid = Guid.NewGuid().ToString(); ;
                result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                result.code = 0;
                result.msg = "成功";
                LogEvent(1, "响应--->EAP下发工位2AGV请求进入命令:  " + JsonConvert.SerializeObject(result));
            }

            if (request.callCode == CommMethods.port3Position)
            {
                LogEvent(1, "EAP下发工位3AGV请求进入命令：" + JsonConvert.SerializeObject(request));
                if (request.method == "IN")
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位3AGV请求.code"], "1");
                CommMethods.port3TaskCode = request.taskCode;
                value = Convert.ToInt16(CommMethods.CurrentMelsecValue["工位3AGV请求.Index"]);
                value = (short)(value + 1);
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位3AGV请求.Index"], value.ToString());
                result.response_uuid = Guid.NewGuid().ToString(); ;
                result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                result.code = 0;
                result.msg = "成功";
                LogEvent(1, "响应--->EAP下发工位3AGV请求进入命令:  " + JsonConvert.SerializeObject(result));
            }


            return result;
        }


        public AgvTellOutResponse AisCenterAgvTellOut(AgvTellOut request)
        {

            AgvTellOutResponse result = new AgvTellOutResponse();

            short value;

            if (request.callCode == CommMethods.port1Position)
            {
                LogEvent(1, "EAP下发工位1AGV搬运完成命令：" + JsonConvert.SerializeObject(request));
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位1AGV请求.code"], "2");
                CommMethods.port1TaskCode = request.taskCode;
                CommMethods.port1ReqCode = request.reqCode;
                value = Convert.ToInt16(CommMethods.CurrentMelsecValue["工位1AGV请求.Index"]);
                value = (short)(value + 1);
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位1AGV请求.Index"], value.ToString());
                result.response_uuid = Guid.NewGuid().ToString(); ;
                result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                result.code = 0;
                result.msg = "成功";
                LogEvent(1, "响应--->EAP下发工位1AGV搬运完成命令:  " + JsonConvert.SerializeObject(result));
            }

            if (request.callCode == CommMethods.port2Position)
            {
                LogEvent(1, "EAP下发工位2AGV搬运完成命令：" + JsonConvert.SerializeObject(request));
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位2AGV请求.code"], "2");
                CommMethods.port2TaskCode = request.taskCode;
                value = Convert.ToInt16(CommMethods.CurrentMelsecValue["工位2AGV请求.Index"]);
                value = (short)(value + 1);
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位2AGV请求.Index"], value.ToString());
                result.response_uuid = Guid.NewGuid().ToString(); ;
                result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                result.code = 0;
                result.msg = "成功";
                LogEvent(1, "响应--->EAP下发工位2AGV搬运完成命令:  " + JsonConvert.SerializeObject(result));
            }

            if (request.callCode == CommMethods.port3Position)
            {
                LogEvent(1, "EAP下发工位3AGV搬运完成命令：" + JsonConvert.SerializeObject(request));
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位3AGV请求.code"], "2");
                CommMethods.port3TaskCode = request.taskCode;
                value = Convert.ToInt16(CommMethods.CurrentMelsecValue["工位3AGV请求.Index"]);
                value = (short)(value + 1);
                CommMethods.CommonMelsecWrite(CommMethods.CurrentMelsecVarList["工位3AGV请求.Index"], value.ToString());
                result.response_uuid = Guid.NewGuid().ToString(); ;
                result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                result.code = 0;
                result.msg = "成功";
                LogEvent(1, "响应--->EAP下发工位3AGV搬运完成命令:  " + JsonConvert.SerializeObject(result));
            }


            return result;
        }




        //EAP主动下发任务接口
        public RecvEapTaskResponse AisRecvEapTask(RecvEapTask request)
        {
            RecvEapTaskResponse result = new RecvEapTaskResponse();
            try
            {
                LogEvent(1, "EAP下发任务存入队列：" + JsonConvert.SerializeObject(request));

                CommMethods.RcvEAP_task_List = request.task_list;

                foreach (var item in CommMethods.RcvEAP_task_List)
                {
                    CommMethods.concurrentQueue.Enqueue(item);
                }
                form.Save();
                form.lotDatagridviewUpdate();
              

                //result.response_uuid = request.request_uuid;
                //result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");     
                result.status = true;
                result.message = "成功";

                LogEvent(1, "响应--->EAP下发任务存入队列:  " + JsonConvert.SerializeObject(result));
            }
            catch (Exception ex)
            {
                result.status = false;
                result.message = $" AisRecvEapTask: {ex.Message}";
                LogEvent(1, "响应--->EAP下发任务存入队列:  " + JsonConvert.SerializeObject(result));
            }
           

            return result;
        }

        //EAP主动下发远程信息接口
        public RecvRemoteHmiMsgResponse RecvRemoteHmiMsg(RecvRemoteHmiMsg request) 
        {

            LogEvent(1, "EAP下发远程信息：" + JsonConvert.SerializeObject(request));
                CIMFrm objCIMFrm = new CIMFrm(request.hmi_msg, request.hmi_dlg_flag=="Y"?"0":"3000"); 
                objCIMFrm.Location = new Point(430, 100);
                //DialogResult Res= objCIMFrm.ShowDialog();
                objCIMFrm.Show();


            RecvRemoteHmiMsgResponse result = new RecvRemoteHmiMsgResponse(); 
            result.response_uuid = request.request_uuid;
            result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            result.code = 0;
            result.msg = "";

            LogEvent(1, "响应--->EAP下发远程信息:  " + JsonConvert.SerializeObject(result));

            return result;
        }


        //EAP测试通讯状态接口
        public ConnTestResponse ConnTest(ConnTest request)
        {

            LogEvent(1, "EAP下发远程信息：" + JsonConvert.SerializeObject(request));
   
            ConnTestResponse result = new ConnTestResponse(); 
            result.response_uuid = request.request_uuid;
            result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            result.code = 0;
            result.msg = "成功";

            LogEvent(1, "响应--->EAP下发远程信息:  " + JsonConvert.SerializeObject(result));

            return result;
        }

        //EAP任务取消接口
        public CancelTaskResponse CancelTask(CancelTask request)
        {

            LogEvent(1, "EAP下发任务取消命令：" + JsonConvert.SerializeObject(request));

            CancelTaskResponse result = new CancelTaskResponse(); 
            result.response_uuid = request.request_uuid;
            result.response_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            result.code = 0;
            result.msg = "";

            LogEvent(1, "响应--->EAP下发任务取消命令:  " + JsonConvert.SerializeObject(result));

            return result;
        }


    }
}
